﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace FetchDataCompare
{
	internal class FetchDataCompare
	{
		public Dictionary<GeoObjectBase, List<GeoObjectStructBase>> CheckData { get; set; }

		public DocumentManger SrcDoc { get; set; }

		public DocumentManger DesDoc { get; set; }

		public List<ReportData> LstReportData { get; set; }

		public FetchDataCompare(DocumentManger srcDoc, DocumentManger desDoc)
		{
			this.CheckData = new Dictionary<GeoObjectBase, List<GeoObjectStructBase>>();
			this.SrcDoc = srcDoc;
			this.DesDoc = desDoc;
			this.LstReportData = new List<ReportData>();
		}

		private bool FetchDataLocate()
		{
			foreach (GeoObjectStructBase geoObjectStructBase in this.SrcDoc.LstGeoObjStruct)
			{
				if (!this.CheckData.ContainsKey(geoObjectStructBase.GeoObject))
				{
					List<GeoObjectStructBase> list = new List<GeoObjectStructBase>();
					list.Add(geoObjectStructBase);
					list.Add(null);
					this.CheckData.Add(geoObjectStructBase.GeoObject, list);
				}
			}
			foreach (GeoObjectStructBase geoObjectStructBase2 in this.DesDoc.LstGeoObjStruct)
			{
				if (this.CheckData.ContainsKey(geoObjectStructBase2.GeoObject))
				{
					List<GeoObjectStructBase> list2 = this.CheckData[geoObjectStructBase2.GeoObject];
					list2[1] = geoObjectStructBase2;
				}
				else
				{
					List<GeoObjectStructBase> list3 = new List<GeoObjectStructBase>();
					list3.Add(null);
					list3.Add(geoObjectStructBase2);
					this.CheckData.Add(geoObjectStructBase2.GeoObject, list3);
				}
			}
			return true;
		}

		private List<ReportData> CheckMove()
		{
			List<ReportData> list = new List<ReportData>();
			IEnumerable<string> source = from lstElems in this.CheckData.Values
			where lstElems[0] != null
			select lstElems[0].GUID;
			List<string> list2 = source.ToList<string>();
			foreach (KeyValuePair<GeoObjectBase, List<GeoObjectStructBase>> keyValuePair in this.CheckData)
			{
				if (keyValuePair.Value[0] == null && keyValuePair.Value[1] != null)
				{
					string guid = keyValuePair.Value[1].GUID;
					ReportData reportData = new ReportData(this.SrcDoc.UseDoc.PathName, this.DesDoc.UseDoc.PathName);
					if (list2.Contains(guid))
					{
						reportData.DesObjectStructBase = keyValuePair.Value[1];
						foreach (KeyValuePair<GeoObjectBase, List<GeoObjectStructBase>> keyValuePair2 in this.CheckData)
						{
							if (keyValuePair2.Value[0] != null && keyValuePair2.Value[0].GUID == keyValuePair.Value[1].GUID)
							{
								reportData.SrcObjectStructBase = keyValuePair.Value[0];
								keyValuePair2.Value[0] = null;
								break;
							}
						}
						keyValuePair.Value[1] = null;
						reportData.CheckType = "修改";
						list.Add(reportData);
					}
				}
			}
			return list;
		}

		private List<ReportData> CheckAdd()
		{
			List<ReportData> list = new List<ReportData>();
			foreach (KeyValuePair<GeoObjectBase, List<GeoObjectStructBase>> keyValuePair in this.CheckData)
			{
				if (keyValuePair.Value[0] == null && keyValuePair.Value[1] != null)
				{
					list.Add(new ReportData(this.SrcDoc.UseDoc.PathName, this.DesDoc.UseDoc.PathName)
					{
						SrcObjectStructBase = keyValuePair.Value[0],
						DesObjectStructBase = keyValuePair.Value[1],
						CheckType = "增加"
					});
				}
			}
			return list;
		}

		private List<ReportData> CheckDel()
		{
			List<ReportData> list = new List<ReportData>();
			foreach (KeyValuePair<GeoObjectBase, List<GeoObjectStructBase>> keyValuePair in this.CheckData)
			{
				if (keyValuePair.Value[1] == null && keyValuePair.Value[0] != null)
				{
					list.Add(new ReportData(this.SrcDoc.UseDoc.PathName, this.DesDoc.UseDoc.PathName)
					{
						SrcObjectStructBase = keyValuePair.Value[0],
						DesObjectStructBase = keyValuePair.Value[1],
						CheckType = "删除"
					});
				}
			}
			return list;
		}

		private List<ReportData> CheckModify()
		{
			List<ReportData> list = new List<ReportData>();
			foreach (KeyValuePair<GeoObjectBase, List<GeoObjectStructBase>> keyValuePair in this.CheckData)
			{
				if (keyValuePair.Value[0] != null && keyValuePair.Value[1] != null && !keyValuePair.Value[0].Equals(keyValuePair.Value[1]))
				{
					list.Add(new ReportData(this.SrcDoc.UseDoc.PathName, this.DesDoc.UseDoc.PathName)
					{
						SrcObjectStructBase = keyValuePair.Value[0],
						DesObjectStructBase = keyValuePair.Value[1],
						CheckType = "修改"
					});
				}
			}
			return list;
		}

		public void DoCompare()
		{
			this.FetchDataLocate();
			this.LstReportData.AddRange(this.CheckMove());
			this.LstReportData.AddRange(this.CheckAdd());
			this.LstReportData.AddRange(this.CheckDel());
			this.LstReportData.AddRange(this.CheckModify());
		}
	}
}
